package com.lianzai.car.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lianzai.camera.model.CameraBlackAndWhiteDO;
import com.lianzai.camera.service.CameraBlackAndWhiteService;
import com.lianzai.common.controller.BaseController;
import com.lianzai.common.executor.LocalSvrSyncExecutor;
import com.lianzai.mobile.dao.CommunityInfoDao;
import com.lianzai.mobile.domain.CarPriceRelaDO;
import com.lianzai.mobile.domain.CarPriceRuleDO;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.mobile.service.CarPriceRelaService;
import com.lianzai.mobile.service.CarPriceRuleService;
import com.lianzai.util.Constants;
import com.lianzai.util.TimeUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianzai.car.domain.CarInfoDO;
import com.lianzai.car.service.CarInfoService;
import com.lianzai.common.utils.PageUtils;
import com.lianzai.common.utils.Query;
import com.lianzai.common.utils.R;

/**
 * 汽车表
 * 
 * @author zb
 * @email 1106900429@qq.com
 * @date 2018-11-30 22:09:40
 */
 
@Controller
@RequestMapping("/car/carInfo")
public class CarInfoController extends BaseController {
	@Autowired
	private CarInfoService carInfoService;

	@Autowired
	private CarPriceRuleService carPriceRuleService;

	@Autowired
	private CarPriceRelaService carPriceRelaService;

	@Autowired
	private CameraBlackAndWhiteService cameraBlackAndWhiteService;

	@Autowired
	private CommunityInfoDao communityInfoDao;

	@GetMapping()
	@RequiresPermissions("car:carInfo:carInfo")
	String CarInfo(){
		return "car/carInfo/carInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("car:carInfo:carInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CarInfoDO> carInfoList = carInfoService.list(query);
		int total = carInfoService.count(query);
		PageUtils pageUtils = new PageUtils(carInfoList, total);
		return pageUtils;
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		Map<String, Object> params = new HashedMap();
		params.put("id", id);
		List<CarInfoDO> list = carInfoService.list(params);
		CarInfoDO cido = list.get(0);
		model.addAttribute("carInfo",cido);
		if(cido.getCommunityId() != null && cido.getCommunityId() != 0){
			//根据小区ID查询小区信息
			CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(cido.getCommunityId()+""));
			if(communityInfoDO.getRuleMonth() != 0){//如果绑定了月租规则
				Map<String, Object> priceQuery = new HashedMap();
				priceQuery.put("type", 1);
				priceQuery.put("id", communityInfoDO.getRuleMonth());
				List<CarPriceRuleDO> priceMonthList = carPriceRuleService.list(priceQuery);
				model.addAttribute("priceMonthList", priceMonthList);
			}
			if(communityInfoDO.getRuleSeason() != 0){//如果绑定了月租规则
				Map<String, Object> priceQuery = new HashedMap();
				priceQuery.put("type", 2);
				priceQuery.put("id", communityInfoDO.getRuleSeason());
				List<CarPriceRuleDO> priceSeasonList = carPriceRuleService.list(priceQuery);
				model.addAttribute("priceSeasonList", priceSeasonList);
			}
			if(communityInfoDO.getRuleYear() != 0){//如果绑定了月租规则
				Map<String, Object> priceQuery = new HashedMap();
				priceQuery.put("type", 3);
				priceQuery.put("id", communityInfoDO.getRuleYear());
				List<CarPriceRuleDO> priceYearList = carPriceRuleService.list(priceQuery);
				model.addAttribute("priceYearList", priceYearList);
			}
		}
		return "car/carInfo/edit";
	}


	@ResponseBody
	@GetMapping("/getPriceRuleList")
	public R getPriceRuleList(@RequestParam("type") Integer type){
		Map<String, Object> priceQuery = new HashedMap();
		priceQuery.put("type", type);
		List<CarPriceRuleDO> priceList = carPriceRuleService.list(priceQuery);
		return R.ok(priceList);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("car:carInfo:add")
	public R save( CarInfoDO carInfo){
		if(carInfoService.save(carInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( CarInfoDO carInfo){
		carInfo.setAuditId(getUserId());
		carInfo.setAuditTime(new Date());
		carInfoService.update(carInfo);
		//这里是和计费规则的关联表，没搞懂审核拒绝为什么要插入这个表数据
		if(carInfo.getAuditStatus() == Constants.AUDIT_PASS){
			CarPriceRelaDO relaDO = new CarPriceRelaDO();
			relaDO.setCarId(carInfo.getId().longValue());
			if(carInfo.getType() == Constants.RULE_MONTH){
				relaDO.setPriceRuleId(carInfo.getPriceMonthId());
			}else if(carInfo.getType() == Constants.RULE_SEASON){
				relaDO.setPriceRuleId(carInfo.getPriceSeasonId());
			}else if(carInfo.getType() == Constants.RULE_YEAR){
				relaDO.setPriceRuleId(carInfo.getPriceYearId());
			}
			relaDO.setCreateTime(new Date());
			relaDO.setCreateId(getUserId());
			carPriceRelaService.save(relaDO);
			//计算白名单起始时间和结束时间，此处修改BY艾超 2019/1/28
			Map map = new HashMap();
			map.put("carNum",carInfo.getCarNo());
			map.put("type",0);
			map.put("communityId",carInfo.getCommunityId());
			map.put("startTime",TimeUtil.queryStartFreeCarTime());
			map.put("endTime",TimeUtil.queryEndFreeCarTime(carInfo.getType().intValue()));
			Map existMap = cameraBlackAndWhiteService.queryExistBlackList(map);//查询下是否存在如果不存在则插入 如果存在则 更新
			int ret = Constants.ERROR;
			if(existMap == null){
				ret = cameraBlackAndWhiteService.save(map);//如果不存在则插入，
			}else{
				ret = cameraBlackAndWhiteService.update2(map);//如果存在则更新
			}
			if(ret == Constants.SUCCESS){//发起下发白名单信息
				CameraBlackAndWhiteDO blackCar = new CameraBlackAndWhiteDO();
				blackCar.setCarNum(carInfo.getCarNo());
				blackCar.setCommunityId(carInfo.getCommunityId());
				blackCar.setCreateTime(new Date());
				blackCar.setType(0);
				blackCar.setStartTime(TimeUtil.queryStartFreeCarTimeParseDate());
				blackCar.setEndTime(TimeUtil.queryEndFreeCarTimeParseDate(carInfo.getType().intValue()));
				CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(carInfo.getCommunityId()+""));
				LocalSvrSyncExecutor.addBlackListCar(blackCar,communityInfoDO);
				return R.ok();
			}else{
				return R.error();
			}
		}
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("car:carInfo:remove")
	public R remove( Integer id){
		if(carInfoService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("car:carInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		carInfoService.batchRemove(ids);
		return R.ok();
	}
	
}
